package com.bbva.kyfb.kyfbjpatlbh.dominiotlbh;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbfuaDataOnDemand.
 */
@Component
@Configurable
@RooDataOnDemand(entity = Ttlsbfua.class)
public class TtlsbfuaDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbfua> data;

	/**
	 * Gets the new transient ttlsbfua.
	 *
	 * @param index the index
	 * @return the new transient ttlsbfua
	 */
	public Ttlsbfua getNewTransientTtlsbfua(int index) {
        Ttlsbfua obj = new Ttlsbfua();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodEstadfic(obj, index);
        setCodUsuario(obj, index);
        setCodUsufirma(obj, index);
        setCodUsupoder(obj, index);
        setFecAccio(obj, index);
        setFecAccion2(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Ttlsbfua obj, int index) {
		TtlsbfuaPK embeddedIdClass;
		if (String.valueOf(index).length() > 7){
			embeddedIdClass = new TtlsbfuaPK("c" + index, "c" + String.valueOf(index).substring(0, 2), "c" + String.valueOf(index).substring(0, 6), new Integer(index).shortValue(), new Integer(index));
		}else{
			embeddedIdClass = new TtlsbfuaPK("c" + index, "c" + index, "c" + index, new Integer(index).shortValue(), new Integer(index));
		}
		
   		obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlsbfua obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlsbfua obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod estadfic.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodEstadfic(Ttlsbfua obj, int index) {
        String codEstadfic = "c_" + index;
        if (codEstadfic.length() > 3) {
            codEstadfic = codEstadfic.substring(0, 3);
        }
        obj.setCodEstadfic(codEstadfic);
    }

	/**
	 * Sets the cod usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodUsuario(Ttlsbfua obj, int index) {
        String codUsuario = "codUsu_" + index;
        if (codUsuario.length() > 8) {
            codUsuario = codUsuario.substring(0, 8);
        }
        obj.setCodUsuario(codUsuario);
    }

	/**
	 * Sets the cod usufirma.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodUsufirma(Ttlsbfua obj, int index) {
        String codUsufirma = "codUsu_" + index;
        if (codUsufirma.length() > 8) {
            codUsufirma = codUsufirma.substring(0, 8);
        }
        obj.setCodUsufirma(codUsufirma);
    }

	/**
	 * Sets the cod usupoder.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodUsupoder(Ttlsbfua obj, int index) {
        String codUsupoder = "codUsu_" + index;
        if (codUsupoder.length() > 8) {
            codUsupoder = codUsupoder.substring(0, 8);
        }
        obj.setCodUsupoder(codUsupoder);
    }

	/**
	 * Sets the fec accio.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setFecAccio(Ttlsbfua obj, int index) {
        String fecAccio = "fecAcc_" + index;
        if (fecAccio.length() > 8) {
            fecAccio = fecAccio.substring(0, 8);
        }
        obj.setFecAccio(fecAccio);
    }

	/**
	 * Sets the fec accion2.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setFecAccion2(Ttlsbfua obj, int index) {
        String fecAccion2 = "fecAcc_" + index;
        if (fecAccion2.length() > 8) {
            fecAccion2 = fecAccion2.substring(0, 8);
        }
        obj.setFecAccion2(fecAccion2);
    }

	/**
	 * Gets the specific ttlsbfua.
	 *
	 * @param index the index
	 * @return the specific ttlsbfua
	 */
	public Ttlsbfua getSpecificTtlsbfua(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbfua obj = data.get(index);
        return Ttlsbfua.findTtlsbfua(obj.getId());
    }

	/**
	 * Gets the random ttlsbfua.
	 *
	 * @return the random ttlsbfua
	 */
	public Ttlsbfua getRandomTtlsbfua() {
        init();
        Ttlsbfua obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbfua.findTtlsbfua(obj.getId());
    }

	/**
	 * Modify ttlsbfua.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbfua(Ttlsbfua obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbfua.findTtlsbfuaEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbfua' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlbh.dominiotlbh.Ttlsbfua>();
        for (int i = 0; i < 10; i++) {
            Ttlsbfua obj = getNewTransientTtlsbfua(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
